<?php

/**
 * Pwiff - PHP SWF Library
 *
 * LICENSE
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @package    Pwiff
 * @subpackage Records
 * @copyright  Copyright (c) 2010 Steven Harris
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version    $Id: CXFormWithAlpha.php 5 2010-02-14 05:08:39Z indigo.sunrise $
 */

/**
 * Represents a color transform with alpha record.
 * 
 * @package    Pwiff
 * @subpackage Records
 */
class Pwiff_Record_CXFormWithAlpha extends Pwiff_Record_CXForm
{
	/**
	 * Alpha multiplication term.
	 *
	 * @var float
	 */
	protected $alphaMultTerm = null;
	
	/**
	 * Alpha addition term.
	 *
	 * @var int
	 */
	protected $alphaAddTerm = null;
	
	/**
	 * Sets the alpha multiplication term.
	 * 
	 * @param  float $alphaMultTerm
	 * @return Pwiff_Record_CXFormWithAlpha
	 */
	public function setAlphaMultTerm($alphaMultTerm)
	{
		if ($alphaMultTerm !== null) {
			$this->verifyTerm($alphaMultTerm * 256);
		}
		$this->alphaMultTerm = $alphaMultTerm;
		return $this;
	}
	
	/**
	 * Sets the alpha addition term.
	 * 
	 * @param  int $alphaAddTerm
	 * @return Pwiff_Record_CXFormWithAlpha
	 */
	public function setAlphaAddTerm($alphaAddTerm)
	{
		$this->verifyTerm($alphaAddTerm);
		$this->alphaAddTerm = $alphaAddTerm;
		return $this;
	}
	
	/**
	 * Gets the alpha multiplication term.
	 * 
	 * @return float
	 */
	public function getAlphaMultTerm()
	{
		return $this->alphaMultTerm;
	}
	
	/**
	 * Gets the alpha addition term.
	 * 
	 * @return int
	 */
	public function getAlphaAddTerm()
	{
		return $this->alphaAddTerm;
	}
	
	/**
	 * Gets the length of this record.
	 *
	 * @return int
	 */
	public function getRecordLength()
	{
		// 6 bits to start
		$bits    = 6;
		$numBits = $this->getBitsNeeded();
		
		// for each term type, there are 4 SB[numBits]
		if ($this->hasMultTerms()) {
			$bits += $numBits * 4;
		}
		if ($this->hasAddTerms()) {
			$bits += $numBits * 4;
		}
		
		return ceil($bits / 8); // round up to full byte
	}
	
	/**
	 * Writes the color transform with alpha values to the given output
	 * instance.
	 *
	 * @param  Pwiff_Output $out
	 */
	public function write(Pwiff_Output $out)
	{
		parent::write($out);
	}
	
	/**
	 * Writes the multiplication terms to the given output instance.
	 * 
	 * @param  Pwiff_Output $out
	 * @param  int $numBits
	 */
	protected function writeMultTerms(Pwiff_Output $out, $numBits)
	{
		parent::writeMultTerms($out, $numBits);
		$out->writeBits((int) (256 * ((float) $this->alphaMultTerm)), $numBits);
	}
	
	/**
	 * Writes the addition terms to the given output instance.
	 * 
	 * @param  Pwiff_Output $out
	 * @param  int $numBits
	 */
	protected function writeAddTerms(Pwiff_Output $out, $numBits)
	{
		parent::writeAddTerms($out, $numBits);
		$out->writeBits((int) $this->alphaAddTerm, $numBits);
	}
	
	/**
	 * Returns true if the record has any multiplication terms.
	 *
	 * @return bool
	 */
	protected function hasMultTerms()
	{	
		return (parent::hasMultTerms() OR $this->alphaMultTerm !== null);
	}
	
	/**
	 * Returns true if the record has any addition terms.
	 *
	 * @return bool
	 */
	protected function hasAddTerms()
	{
		return (parent::hasAddTerms() OR $this->alphaAddTerm !== null);
	}
	
	/**
	 * Returns the number of bits needed to represent the CXFORMWITHALPHA
	 * record's values.
	 *
	 * @return int
	 */
	protected function getBitsNeeded()
	{
		$numbers = array();
		
		if ($this->hasAddTerms()) {
			$numbers[] = (int) $this->redAddTerm;
			$numbers[] = (int) $this->greenAddTerm;
			$numbers[] = (int) $this->blueAddTerm;
			$numbers[] = (int) $this->alphaAddTerm;
		}
		
		if ($this->hasMultTerms()) {
			$numbers[] = (int) (256 * ((float) $this->redMultTerm));
			$numbers[] = (int) (256 * ((float) $this->greenMultTerm));
			$numbers[] = (int) (256 * ((float) $this->blueMultTerm));
			$numbers[] = (int) (256 * ((float) $this->alphaMultTerm));
		}
		
		return Pwiff_Util_Numerics::getSignedBitCount($numbers);
	}
}
